#!/usr/bin/env perl
# https://github.com/shenwei356/bio_scripts

use strict;
use File::Basename;
use BioUtil::Seq;
use BioUtil::Util;

my $usage = q(
auto_makeblastdb -- just for typing fewer words..^_^

Usage: $0 [arguments]
Options:    
    -in                  
    -input_type          [fasta]

    -dbtype              [nucl]
    -out
    -title               

    -parse_seqids        [false]

    -mask                [false]

    -h                   Show this help information

Examples:

    auto_makeblastdb -in ab.fa -out ab

https://github.com/shenwei356/bio_scripts

);

my $opts = {};

getopt( $opts, \@ARGV );

die $usage unless exists $$opts{-in} and exists $$opts{-out};
die "file not exists: $$opts{-in}\n" unless -e $$opts{-in};

$$opts{-title} = $$opts{-out} unless exists $$opts{-title};
$$opts{-dbtype} = 'nucl' unless exists $$opts{-dbtype};
$$opts{-input_type} = 'fasta' unless exists $$opts{-input_type};

my $cmd = '';
my ( $file_mask_asnb, $file_mask_counts ) = (undef) x 2;
if ( $$opts{-mask} ) {
    print STDERR "Create masking information using windowmask...\n";
    $file_mask_counts = "$$opts{-out}_mask.counts";
    $file_mask_asnb   = "$$opts{-out}_mask.asnb";
    $cmd
        = sprintf
        "windowmasker -in %s -infmt %s -mk_counts -out %s",
        quotemeta $$opts{-in}, $$opts{-input_type}, $file_mask_counts;
    $cmd .= " -parse_seqids" if $$opts{-parse_seqids};
    my $fail = run($cmd);
    die "failed to run:$cmd\n" if $fail;

    print STDERR "Makeblastdb...\n";
    $cmd
        = sprintf
        "windowmasker -in %s -infmt %s -ustat %s -outfmt %s -out %s",
        quotemeta $$opts{-in}, $$opts{-input_type}, $file_mask_counts,
        "maskinfo_asn1_bin", $file_mask_asnb;    
    $cmd .= " -parse_seqids" if $$opts{-parse_seqids};
    
    my $fail = run($cmd);
    die "failed to run:$cmd\n" if $fail;
}

$cmd
    = sprintf
    "makeblastdb -in %s -input_type %s -dbtype %s -out %s -title %s",
    quotemeta $$opts{-in}, $$opts{-input_type}, $$opts{-dbtype},
    $$opts{-out}, $$opts{-title};
$cmd .= " -parse_seqids" if $$opts{-parse_seqids};

if ( $$opts{-mask} ) {
    $cmd .= " -mask_data $file_mask_asnb";
}

print STDERR "$cmd\n";
my $fail = run($cmd);
die "failed to run:$cmd\n" if $fail;
